﻿<script>
    window.DHCar_APIServer = "http://api2.dhcar.com/"
</script>
<div class="modal fade in" style="display:block;background-color:rgba(0,0,0,.3);" id="DHCar_ImportTemplate">
    <div class="modal-dialog modal-lg" style="width:95%;margin-top:30px;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" onclick="DHCar.Close()" style="height:40px;width:90px;"></button>
                <h4 class="modal-title">车享家导出程序-南京东华</h4>
            </div>
            <div class="modal-body form-horizontal" id="DHCar_ModalBody" style="overflow:auto;height:500px;">
                <table class="table table-bordered" style="width:100%;">
                    <thead>
                        <tr style="background-color:green;color:#fff;">
                            <th style="width:350px;">查询条件(结算日期)</th>
                            <th style="width:250px;">操作按钮</th>
                            <th style="width:200px;">解析结果(订单数量)</th>
                            <th style="width:120px;">完善进度</th>
                            <th style="width:120px;">上报进度</th>
                            <th>消息状态</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                <div class="input-group date-picker input-daterange" data-date-format="yyyy-mm-dd">
                                    <input type="text" class="form-control padding-0" id="DHCar_StartDate">
                                    <span class="input-group-addon">to</span>
                                    <input type="text" class="form-control padding-0" id="DHCar_EndDate">
                                </div>
                            </td>
                            <td>
                                <a class="btn btn-primary" id="DHCar_btnAnalysis" onclick="DHCar.StartAnalysis()">开始解析</a>
                                <a class="btn btn-primary" disabled="disabled" id="DHCar_btnUpload" onclick="DHCar.StartUpload()">上报到东华</a>
                            </td>
                            <td style="font-weight:bolder;">
                                <span style="color:green;" name="DHCar_OrderTotal">---</span> 单
                            </td>
                            <td style="font-weight:bolder;">
                                <span name="DHCar_UpdateProcess">---</span>/<span name="DHCar_OrderTotal">---</span>
                            </td>
                            <td style="font-weight:bolder;">
                                <span name="DHCar_UploadProcess">---</span>/<span name="DHCar_OrderTotal">---</span>
                            </td>
                            <td style="font-weight:bolder;">
                                <span id="DHCar_Msg" style="color:red;"></span>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <p style="border:1px solid green;width:100%;margin:10px 0px;box-shadow:1px 1px 1px rgba(0,0,0,.3);"></p>
                <table class="table table-striped table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>序号</th>
                            <th>订单编号</th>
                            <th>订单ID</th>
                            <th>门店名称</th>
                            <th>客户姓名</th>
                            <th>客户电话</th>
                            <th>订单金额</th>
                            <th>车牌号</th>
                            <th>车品牌</th>
                            <th>里程表读数</th>
                            <th>维修日期</th>
                            <th>维修类别</th>
                            <th>维修记录简单描述</th>
                        </tr>
                    </thead>
                    <tbody id="DHCar_OrderTableList"></tbody>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" onclick="DHCar.Close()">关闭</button>
            </div>
        </div>
    </div>
</div>
<script>
    function DHCar_getDate(number) {
        var dt = new Date();
        if (number) {
            dt = new Date(dt.getTime() + 24 * 60 * 60 * 1000 * number);
        }
        return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate();
    }


    var DHCar = {};
    $.extend(DHCar, {
        StateConfig: {
            StartDate: DHCar_getDate(-4),
            EndDate: DHCar_getDate(-4)
        },
        ResetConfig: function () {

            $("#DHCar_OrderTableList").html('');
            $("#DHCar_Msg").text('');

            $.extend(this.StateConfig, {
                AnalysisEnable: true,
                UploadEnable: false,
                OrderTotal: '---',
                UpdateProcess: '---',
                UploadProcess: '---',
                OrderList: [],
                OrderIndex: 1
            });

            this.ShowStateConfig();
        },
        UpdateConfig: function (obj) {
            $.extend(this.StateConfig, obj);
            this.ShowStateConfig();
        },
        Init: function () {
            $("#DHCar_ModalBody").css({ 'height': (document.documentElement.clientHeight - 220) + "px" });
            $("#DHCar_Msg").css({ 'height': (document.documentElement.clientHeight - 280) + "px" });

            $("#DHCar_StartDate").val(this.StateConfig.StartDate);
            $("#DHCar_EndDate").val(this.StateConfig.EndDate);

            this.ResetConfig();
        },
        Close: function () {
            DHCar = null;
            $("#DHCar_ImportTemplate").remove();
        },
        ShowStateConfig: function () {
            if (this.StateConfig.AnalysisEnable) {
                $("#DHCar_btnAnalysis").removeAttr("disabled");
            } else {
                $("#DHCar_btnAnalysis").attr("disabled", "disabled");
            }
            if (this.StateConfig.UploadEnable) {
                $("#DHCar_btnUpload").removeAttr("disabled");
            } else {
                $("#DHCar_btnUpload").attr("disabled", "disabled");
            }

            $("#DHCar_ModalBody").find("span[name='DHCar_OrderTotal']").text(this.StateConfig.OrderTotal);
            $("#DHCar_ModalBody").find("span[name='DHCar_UpdateProcess']").text(this.StateConfig.UpdateProcess);
            $("#DHCar_ModalBody").find("span[name='DHCar_UploadProcess']").text(this.StateConfig.UploadProcess);
        },
        StartAnalysis: function (pageIndex) {

            if (!pageIndex) {
                pageIndex = 1;
                this.ResetConfig();
            }
            DHCar.ShowMessage("正在解析第【" + pageIndex + "】页数据....");

            this.UpdateConfig({ AnalysisEnable: false });

            $.ajax({
                type: 'post',
                dataType: 'html',
                url: 'http://jad.chexiang.com/cs/ord/toQueryOrderManage.htm',
                data: {
                    storeIds: '',
                    orderNo: '',
                    custName: '',
                    custPhone: '',
                    orderTimeFrom: '',
                    orderTimeTo: '',
                    plates: '',
                    tmpPlate: '',
                    invoiceStatus: '',
                    orderType: 1,
                    orderStatus: 2,
                    settlementTimeFrom: $("#DHCar_StartDate").val(),
                    settlementTimeTo: $("#DHCar_EndDate").val(),
                    storeName: '',
                    couponNo: '',
                    thirdOrderNo: '',
                    orderSource: '',
                    'pager.currentPage': pageIndex,
                    goSize: '',
                },
                success: function (response) {
                    DHCar.AnalysisHtml(response, pageIndex);
                },
                error: function () {
                    DHCar.ShowMessage("车享家服务器出错,解析终止...");
                }
            })
        },
        AnalysisHtml: function (response, pageIndex) {
            var s1 = $(response);
            var s2 = s1.find("#orderMgmtTab tbody tr");
            if (s2.length > 0) {
                var _orderList = [];
                $.each(s2, function (index, ele) {
                    var _order = {
                        OrderId: _getOrderId($(ele).find('td').eq(0).find("a").attr("onclick")),
                        OrderNo: $(ele).find('td').eq(0).find("a").text(),
                        StoreName: $(ele).find('td').eq(1).text(),
                        CustomerName: $(ele).find('td').eq(2).text(),
                        Mobile: $(ele).find('td').eq(3).text(),
                        Cost: $(ele).find('td').eq(4).text(),
                        CarNo: $(ele).find('td').eq(7).text(),
                        CarBrand: '',
                        Milage: '',
                        RepairDate: '',
                        RepairType: '养修',
                        RepairDesc: ''
                    };
                    DHCar.StateConfig.OrderList.push(_order);
                    _orderList.push(_order);
                })

                DHCar.BindingTable(_orderList);
                this.UpdateConfig({ OrderTotal: DHCar.StateConfig.OrderList.length });

                //DHCar.UpdateOrderInfo();
                DHCar.StartAnalysis(pageIndex + 1);

            } else {
                DHCar.UpdateOrderInfo();
            }


            function _getOrderId(str) {
                if (!str) return '';
                return str.replace('popOrderBaseReserve(\'', '').replace('\');', '');
            }
        },
        UpdateOrderInfo: function (cIndex) {
            if (DHCar.StateConfig.OrderList.length == 0) return;
            if (!cIndex) cIndex = 0;
            if (DHCar.StateConfig.OrderList.length == cIndex) {

                this.UpdateConfig({ UploadEnable: true });
                DHCar.ShowMessage("3秒后自动上报到东华数据中心");

                setTimeout(function () {
                    DHCar.ShowMessage("2秒后自动上报到东华数据中心");

                    setTimeout(function () {
                        DHCar.ShowMessage("1秒后自动上报到东华数据中心");

                        setTimeout(function () {
                            DHCar.ShowMessage("");
                            DHCar.StartUpload();

                        }, 1000);

                    }, 1000);

                }, 1000);

            } else {
                this.UpdateConfig({ UpdateProcess: cIndex + 1 });
                DHCar.ShowMessage("正在更新第【" + (cIndex + 1) + "】个数据....");

                var _order = DHCar.StateConfig.OrderList[cIndex];

                if (!_order.OrderId) {

                    DHCar.UpdateOrderInfo(cIndex + 1);

                } else {
                    $.ajax({
                        type: 'get',
                        url: 'http://jad.chexiang.com/cs/ord/queryOrderDetail.htm?orderId=' + _order.OrderId,
                        dataType: 'html',
                        success: function (response) {

                            var s1 = $(response);
                            _order.CarBrand = s1.find(".modal-body").find("input[type='text']").filter(".form-control").eq(13).val()
                            _order.Milage = s1.find(".modal-body").find("input[type='text']").filter(".form-control").eq(21).val()
                            _order.RepairDate = s1.find(".modal-body").find("input[type='text']").filter(".form-control").eq(3).val()

                            if (s1.find(".modal-body").find("#collapseOne tbody tr").length != 0) {
                                _order.RepairDesc = s1.find(".modal-body").find("#collapseOne tbody tr").eq(0).find("td").eq(0).text();
                            }

                            if (_order.RepairDate) {
                                _order.RepairDate = _order.RepairDate.substring(0, 10);
                            }

                            $("#DHCar_TR_" + _order.OrderNo).find('td[name="CarBrand"]').text(_order.CarBrand);
                            $("#DHCar_TR_" + _order.OrderNo).find('td[name="Milage"]').text(_order.Milage);
                            $("#DHCar_TR_" + _order.OrderNo).find('td[name="RepairDate"]').text(_order.RepairDate);
                            $("#DHCar_TR_" + _order.OrderNo).find('td[name="RepairDesc"]').text(_order.RepairDesc);

                            DHCar.UpdateOrderInfo(cIndex + 1);

                        },
                        error: function () {
                            DHCar.ShowMessage("车享家服务器出错,更新终止...");
                        }
                    })
                }
            }
        },
        StartUpload: function (cIndex) {
            if (DHCar.StateConfig.OrderList.length == 0) return;
            if (!cIndex) {
                cIndex = 0;
                this.UpdateConfig({ UploadEnable: false });
            }
            if (DHCar.StateConfig.OrderList.length == cIndex) {

                this.UpdateConfig({ AnalysisEnable: true });
                DHCar.ShowMessage("全部上报完成");

            } else {
                DHCar.ShowMessage("正在上报第【" + (cIndex + 1) + "】个数据....");

                var _order = DHCar.StateConfig.OrderList[cIndex];

                if (!_order.OrderId) {

                    DHCar.StartUpload(cIndex + 1);

                } else {
                    $.ajax({
                        type: 'post',
                        url: window.DHCar_APIServer + 'api/cxj/jad/upload',
                        data: JSON.stringify(_order),
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (response) {
                            if (response.success == true) {
                                DHCar.UpdateConfig({ UploadProcess: cIndex + 1 });
                                $("#DHCar_TR_" + _order.OrderNo).hide();
                                DHCar.StartUpload(cIndex + 1);
                            } else {
                                DHCar.ShowMessage("东华服务出错,上报终止...");
                                DHCar.UpdateConfig({ UploadEnable: true });
                            }
                        },
                        error: function () {
                            DHCar.ShowMessage("东华服务出错,上报终止...");
                            DHCar.UpdateConfig({ UploadEnable: true });
                        }
                    })
                }
            }
        },
        BindingTable: function (orderList) {
            $.each(orderList, function (index, ele) {
                $("#DHCar_OrderTableList").append('<tr id="DHCar_TR_' + ele.OrderNo + '">'
                    + '<td>' + (DHCar.StateConfig.OrderIndex++) + '</td>'
                    + '<td>' + ele.OrderNo + '</td>'
                    + '<td>' + ele.OrderId + '</td>'
                    + '<td>' + ele.StoreName + '</td>'
                    + '<td>' + ele.CustomerName + '</td>'
                    + '<td>' + ele.Mobile + '</td>'
                    + '<td>' + ele.Cost + '</td>'
                    + '<td>' + ele.CarNo + '</td>'
                    + '<td name="CarBrand">' + ele.CarBrand + '</td>'
                    + '<td name="Milage">' + ele.Milage + '</td>'
                    + '<td name="RepairDate">' + ele.RepairDate + '</td>'
                    + '<td>' + ele.RepairType + '</td>'
                    + '<td name="RepairDesc">' + ele.RepairDesc + '</td>'
                    + '</tr>')
            })
        },
        ShowMessage: function (msg) {
            $("#DHCar_Msg").text(msg);
        }
    })
    DHCar.Init();
</script>